package ui.clustering;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.util.ArrayList;

import edu.uci.ics.jung.graph.impl.DirectedSparseVertex;

public class ClusterVertex extends DirectedSparseVertex
{
    public final static Color[] GRADIENT_COLOR       = {
            new Color(24, 97, 254), // new Color(20, 131, 246),
            new Color(24, 160, 246), // new Color(30, 210, 245),
            new Color(36, 241, 234), // new Color(67, 255, 200),
            new Color(106, 251, 176), // new Color(139, 255, 121),
            new Color(178, 248, 100), // new Color(205, 238, 73),
            new Color(221, 232, 44), // new Color(254, 216, 29),
            new Color(252, 174, 16), // new Color(255, 132, 26),
            new Color(251, 82, 13), // new Color(244, 55, 13),
            new Color(222, 42, 9), // new Color(196, 30, 18),
                                                     };

    public static Dimension     defalutSize          = new Dimension(800, 600);
    public static Paint         defaultDrawPaint     = Color.black;
    public static int           defaultMaxVertexSize = 60;
    public static int           defaultMinVertexSize = 10;
    private static int          maxSize              = -1;
    private String              center;
    private ArrayList<String>   cluster;
    private String              info;

    public ClusterVertex(String center, ArrayList<String> cluster)
    {
        super();
        this.center = center;
        this.cluster = cluster;
        if (cluster.size() > maxSize)
            maxSize = cluster.size();
    }

    /**
     * @return the center
     */
    public String getCenter()
    {
        return center;
    }

    /**
     * @return the cluster
     */
    public ArrayList<String> getCluster()
    {
        return cluster;
    }

    /**
     * @param center
     *            the center to set
     */
    public void setCenter(String center)
    {
        this.center = center;
    }

    /**
     * @param cluster
     *            the cluster to set
     */
    public void setCluster(ArrayList<String> cluster)
    {
        this.cluster = cluster;
        if (cluster.size() > maxSize)
            maxSize = cluster.size();
    }

    /**
     * @return the shape
     */
    public Shape getShape()
    {
        double base = (defaultMaxVertexSize - defaultMinVertexSize) * 1.0
                * cluster.size() / maxSize + defaultMinVertexSize;
        Ellipse2D shape = new Ellipse2D.Double(-base / 2, -base / 2, base, base);
        return shape;
    }

    /**
     * @return the fillPaint
     */
    public Paint getFillPaint()
    {
        double base = (1 - 1.0 * cluster.size() / maxSize)
                * GRADIENT_COLOR.length - 1E-10;
        return GRADIENT_COLOR[(int) base];
        // return GRADIENT_COLOR[0];
    }

    /**
     * @return the drawPaint
     */
    public Paint getDrawPaint()
    {
        return defaultDrawPaint;
    }

    /**
     * @return the info
     */
    public String getInfo()
    {
        return center;
    }

    /**
     * @param info
     *            the info to set
     */
    public void setInfo(String info)
    {
        this.info = info;
    }

    @Override
    public String toString()
    {
        if (info == null)
        {
            info = "center:" + getCenter() + "; size:" + cluster.size() + " ["
                    + cluster.get(0) + ","
                    + (cluster.size() > 1 ? cluster.get(1) + "," : "")
                    + (cluster.size() > 2 ? cluster.get(2) + "," : "")
                    + (cluster.size() > 3 ? cluster.get(3) : "")
                    + (cluster.size() > 4 ? "..." : "") + "]";
        }
        return info;
    }
}
